<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/topcategory/' + goods.categories[1].id">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="'/topcategory/sub/' + goods.categories[0].id">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
          <GoodName :goods="goods"></GoodName>
          <GoodsSku :goods="goods" skuId="" @change="changeSku"></GoodsSku>
          <!-- 商品数量 -->
          <XtxNumbox v-model="num" :max="goods.inventory" label="数量"></XtxNumbox>
          <!-- 按钮 -->
          <XtxButton type="primary" size="middle" style="margin-top:20px" @click="insertCart()">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant v-if="goods" :goodsId="goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->

          <div class="goods-tabs">
 <goodsTabs></goodsTabs>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <goodsWarm></goodsWarm>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goodsHot ></goodsHot>
           <goodsHot :type='2'></goodsHot>
        </div>
      </div>
    </div>
    <div class="loading" v-else></div>
  </div>
</template>
<script>
import { useRoute } from 'vue-router'
import GoodsRelevant from './components/goods-relevant'
import { findGoods } from '@/api/product'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import goodsTabs from './components/goods-tabs.vue'
import goodsHot from './components/goods-hot.vue'
import goodsWarm from './components/goods-warm.vue'
import { nextTick, provide, ref, watch } from 'vue'
import { useStore } from 'vuex'
import Message from '@/components/library/message'

export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodName, GoodsSku, goodsTabs, goodsHot, goodsWarm },
  setup () {
    const num = ref(1)
    const loading = ref(false)
    const store = useStore()
    const useGoods = () => {
      const goods = ref()
      console.log(goods)
      const route = useRoute()
      console.log(route.params.id)
      watch(
        () => route.params.id,
        (newValue) => {
          if (newValue) {
            loading.value = true
            findGoods(route.params.id).then((data) => {
              // console.log(data)
              goods.value = null
              nextTick(() => {
                goods.value = data.result
                loading.value = false
              })
            })
          }
        },
        { immediate: true }
      )
      return goods
    }
    const goods = useGoods()
    const changeSku = (skuGoods) => {
      // 根据此数据得到所选规格商品信息。eg：所选规格价格原价....
      if (skuGoods.skuId) {
        console.log(skuGoods);
        goods.value.price = skuGoods.price
        goods.value.oldPrice = skuGoods.oldPrice
        goods.value.inventory = skuGoods.inventory
        // currSku.value = skuGoods
      } 
      //当你选择完有没有数据都在这赋值
      console.log(123445678,skuGoods);
        currSku.value = skuGoods
    }
    provide('goods', goods)
    // 点击加入购物车
    const currSku = ref('null')
    const insertCart = () => {
      console.log(currSku.value);
      // es6解构赋值重命名specsText:attrsText,inventory:stock
      const { skuId, specsText: attrsText, inventory: stock } = currSku.value
      const { price, id, name, mainPictures,oldPrice, } = goods.value
      if (currSku.value && currSku.value.skuId) {
        store.dispatch('car/insertCart', {
         oldPrice,
          skuId,
          attrsText,
          stock,
          nowPrice: price,
          id,
          name,
          picture: mainPictures[0],
          selected: true,
          isEffective: true,
          count: num.value
        })
        Message({type:'success',text:'加入购物车成功'})
      }else{
        Message({text:'请选择完整规格'})
        console.log(1111);
      }
    }
    return { goods, loading, changeSku, num, currSku, insertCart }
  }
}
</script>

<style scoped lang='less'>
.loading {
  height: 580px;
  width: 1240px;
  margin: 72px auto 20px;
  background: hsla(0, 0%, 100%, 0.9) url(../../assets/images/loading.gif)
    no-repeat center;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    padding: 30px 30px 30px 0;
    flex: 1;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
